/*! copyright (c) 2015 cyg */
seajs.use(["mmj-jquery", "confirm-box", "modules/common/my-style", "widget", "cookie"], function($, ConfirmBox, Style, Widget, Cookie) {
    function updateCollect(collectBtn) {
        $.get(BASE_PATH + "/goods/checkisfavor", {
            goods_id: collectBtn.data().goodsId,
            t: (new Date).getTime()
        }, function(data) {
            var btn = collectBtn[0]
                , btnData = collectBtn.data();
            data = eval("(" + data + ")"),
            "noFavor" == data.code && (btn.className = "btn-collect"),
            "hasFavor" == data.code && (btn.className = "btn-collect collect-active",
                btn.title = "\u70b9\u51fb\u53d6\u6d88\u5173\u6ce8",
                btnData.isFavor = "1"),
                btn.innerHTML = "( " + data.count + " )"
        })
    }
    (function() {
            var e, t = $(".J-message"), o = "", n = $(".user .email").html(), a = "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br>\u6e29\u99a8\u63d0\u793a<br>1.\u8d2d\u4e70\u89d2\u8272\u65f6\u4e0d\u53ef\u767b\u5f55\u6e38\u620f\u3002<br>2.\u65f6\u6548\u6027\u7269\u54c1\u4e3a\u4e0a\u67b6\u51fa\u552e\u65f6\u7684\u72b6\u6001\uff0c\u8d2d\u4e70\u540e\u53ef\u80fd\u56e0\u4e3a\u65f6\u95f4\u95ee\u9898\u65e0\u6cd5\u7ee7\u7eed\u4f7f\u7528\u3002";
            $.each(t, function(e, t) {
                o += t.innerHTML + "<br>"
            }),
                $("#buySubmit").click(function() {
                    var t = $.trim($("#goods_serial_num").val());
                    if (!t.length)
                        return ConfirmBox.warning("\u8be5\u5546\u54c1\u4e0d\u5b58\u5728\uff01"),
                            !1;
                    var i = '<div class="dialog-upper">' +
                        '<p class="dialog-text dialog-text-lefter"><b>您将购买角色至账号<span class="email">' + n + "</span></b><br><br>" + o + a + '</p>' +
                        '</div>' +
                        '<div id="J_codeWrap"><input id="J_attackCodeVal" type="text" class="attackCode" placeholder="\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801"/>' +
                        '<a class="attackCodeToggle" href="javascript:void(0);"><img width="92" height="24" id="J_attackCode" class="J_attackCode" src="' + BASE_PATH + "/transaction/captcha-image?goods_serial_num=" + t + "&t=" + (new Date).getTime() + '" alt="防刷验证码" /></a><a class="J_attackCode" href="javascript:void(0);">换一张</a></div>';
                    return e = ConfirmBox.notice(i, "立即购买", "下单并支付", function() {
                        var o = $.trim($("#J_attackCodeVal").val());
                        if ("" == o)
                            return e.element.remove(),
                                ConfirmBox.warning("请输入验证码！").show(),
                                !1;
                        var n = e.element.find("[data-role=confirm]");
                        n.find(".span").html("系统处理中"),
                            $.ajax({
                                url: BASE_PATH + "/transaction/buy",
                                type: "post",
                                async: !1,
                                data: {
                                    goods_serial_num: t,
                                    captcha_code: o
                                },
                                success: function(t) {
                                    if (0 == t.indexOf("success_")) {
                                        var o = t.substring("success_".length);
                                        return n.find(".span").html("\u6210\u529f\u8df3\u8f6c\u4e2d"),
                                            window.location.href = BASE_PATH + "/transaction/check?orders_num=" + o,
                                            !1
                                    }
                                    return e.hide(),
                                    "404" == t && ConfirmBox.warning("\u8be5\u5546\u54c1\u4e0d\u5b58\u5728\uff01"),
                                    "too_much_orders" == t && ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u60a8\u540c\u65f6\u53ea\u80fd\u8d2d\u4e70\u4e00\u4e2a\u89d2\u8272\uff0c<br>\u8bf7\u70b9\u51fb\u201c\u67e5\u770b\u8ba2\u5355\u201d\u5904\u7406\u5df2\u6709\u8ba2\u5355\u3002", "\u8d2d\u4e70\u63d0\u793a", function() {
                                        window.location.href = BASE_PATH + "/transaction/bought/goodslist"
                                    }, "\u67e5\u770b\u8ba2\u5355"),
                                    "seller_is_locked" == t && ConfirmBox.warning("\u62b1\u6b49\uff01\u6b64\u5546\u54c1\u5b58\u5728\u88ab\u76d7\u98ce\u9669\uff0c\u65e0\u6cd5\u88ab\u8d2d\u4e70\u6216\u9884\u8ba2\uff01", "\u63d0\u793a", function() {
                                        window.close()
                                    }),
                                    "canot_buy_self" == t && ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u60a8\u4e0d\u80fd\u8d2d\u4e70\u81ea\u5df1\u5bc4\u552e\u7684\u89d2\u8272\u3002"),
                                    "seller_is_xiaohu" == t && ConfirmBox.warning("\u62b1\u6b49\uff01\u6b64\u5546\u54c1\u5b58\u5728\u8d44\u91d1\u98ce\u9669\uff0c\u65e0\u6cd5\u88ab\u8d2d\u4e70\u6216\u9884\u8ba2\uff01"),
                                    "buyer_is_abnormal" == t && ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u60a8\u7684\u64cd\u4f5c\u5b58\u5728\u5f02\u5e38\u3002"),
                                    "canot_buy" == t && ConfirmBox.warning("\u62b1\u6b49\uff01\u6b64\u5546\u54c1\u65e0\u6cd5\u88ab\u8d2d\u4e70"),
                                    "too_much_chars" == t && ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u60a8\u5728\u8be5\u533a\u670d\u7684\u89d2\u8272\u6570\u91cf\u5df2\u6ee1\uff0c\u65e0\u6cd5\u8d2d\u4e70\uff0c\u8bf7\u786e\u8ba4\u5728\u8be5\u670d\u52a1\u5668\u4e0a\u7559\u6709\u81f3\u5c11\u4e00\u4e2a\u89d2\u8272\u7a7a\u4f4d\u3002"),
                                    "need_logout_in_game" == t && ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u6e38\u620f\u5728\u7ebf\u65e0\u6cd5\u8fdb\u884c\u8d2d\u4e70\uff0c\u8bf7\u9000\u51fa\u6e38\u620f\u540e\u518d\u8bd5\u3002\uff08\u82e5\u6e38\u620f\u5df2\u7ecf\u9000\u51fa\uff0c\u53ef\u80fd\u4e3a\u7cfb\u7edf\u5ef6\u8fdf\uff0c\u8bf7\u51e0\u5206\u949f\u540e\u518d\u8bd5\u3002\uff09"),
                                    "service_not_available" == t && ConfirmBox.warning('\u5bf9\u4e0d\u8d77\uff0c\u670d\u52a1\u5f02\u5e38\uff0c\u65e0\u6cd5\u8d2d\u4e70\uff01<br>\u5982\u591a\u6b21\u672a\u6210\u529f\uff0c\u8bf7<a target="_blank" href="' + BASE_PATH + '/advice/index">\u8054\u7cfb\u6211\u4eec</a>'),
                                    "fail" == t && ConfirmBox.warning('\u7cfb\u7edf\u5904\u7406\u5931\u8d25\uff0c\u8bf7\u60a8\u7a0d\u540e\u518d\u8bd5\u3002<br>\u5982\u591a\u6b21\u672a\u6210\u529f\uff0c\u8bf7<a target="_blank" href="' + BASE_PATH + '/advice/index">\u8054\u7cfb\u6211\u4eec</a>'),
                                    "cancel_order_limit" == t && ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u60a8\u7684\u8d26\u53f7\u6216\u6240\u5728IP\u65e0\u6cd5\u8d2d\u4e70\u6b64\u5546\u54c1 <br> \u8ba2\u5355\u53d6\u6d88\u540e2\u5c0f\u65f6\u5185\u4e0d\u80fd\u518d\u6b21\u8ba2\u8d2d\u8be5\u5546\u54c1\u3002"),
                                    "cancel_too_much" == t && ConfirmBox.warning("\u60a8\u7684\u8d26\u53f7\u6216\u6240\u5728IP\u56e0\u8fc7\u4e8e\u9891\u7e41\u4e0b\u5355\u548c\u53d6\u6d88\u8ba2\u5355\uff0c\u5df2\u88ab\u7cfb\u7edf\u9501\u5b9a\uff0c\u5c06\u4e8e2\u5929\u5185\u4e0d\u80fd\u5728\u7545\u6613\u9601\u4e0b\u5355"),
                                    "site_is_maintance" == t && ConfirmBox.warning('\u6b63\u5728\u7ef4\u62a4\u4e2d\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\u3002<br>\u5982\u6709\u95ee\u9898\uff0c\u8bf7<a target="_blank" href="' + BASE_PATH + '/advice/index">\u8054\u7cfb\u6211\u4eec</a>'),
                                    "game_is_maintance" == t && ConfirmBox.warning('\u5bf9\u4e0d\u8d77\uff0c\u6e38\u620f\u6b63\u5728\u7ef4\u62a4\uff01<br>\u5982\u6709\u95ee\u9898\uff0c\u8bf7<a target="_blank" href="' + BASE_PATH + '/advice/index">\u8054\u7cfb\u6211\u4eec</a>'),
                                    "has_been_ordered" == t && ConfirmBox.warning("\u6765\u665a\u4e00\u6b65\u54e6\uff0c\u5546\u54c1\u5df2\u88ab\u4e0b\u5355\u55bd\u3002", "", function() {
                                        window.location.reload()
                                    }),
                                    "cannot_buy_any" == t && ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u60a8\u7684\u8d26\u53f7\u6216\u6240\u5728IP\u5b58\u5728\u8d2d\u4e70\u64cd\u4f5c\u5f02\u5e38\u98ce\u9669\uff0c2\u5c0f\u65f6\u5185\u4e0d\u80fd\u518d\u6b21\u8d2d\u4e70\u8be5\u5546\u54c1\u3002"),
                                    "buyer_is_locked" == t && ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u60a8\u7684\u8d26\u53f7\u5b58\u5728\u5f02\u5e38\uff0c\u65e0\u6cd5\u8fdb\u884c\u8be5\u64cd\u4f5c "),
                                    "captcha_error" == t && (e.element.remove(),
                                        ConfirmBox.warning("\u9a8c\u8bc1\u7801\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002").show()),
                                    "dj_cannot_buy" == t && new ConfirmBox({
                                        title: "\u6e29\u99a8\u63d0\u793a",
                                        width: 360,
                                        confirmText: "\u9000\u51fa",
                                        cancelText: "\u53d6\u6d88",
                                        content: '<div class="dialog-main"><i class="dialog-icon warnning"></i><p class="dialog-text">\u60a8\u76ee\u524d\u767b\u5f55\u7684\u662f\u201c\u5200\u5251\u8d26\u53f7\u201d\uff0c\u65e0\u6cd5\u8fdb\u884c\u5200\u5251\u5916\u7684\u6e38\u620f\u5546\u54c1\u8d2d\u4e70\uff0c\u5efa\u8bae\u60a8\u5207\u6362\u4e3a\u7545\u6e38\u8d26\u53f7\u540e\u518d\u8fdb\u884c\u8d2d\u4e70\u64cd\u4f5c\u3002</p></div>',
                                        onConfirm: function() {
                                            window.location.href = BASE_PATH + "/logout"
                                        },
                                        onClose: function() {
                                            this.hide()
                                        }
                                    }).show(),
                                        !1
                                }
                            })
                    }),
                        e.after("show", function() {
                            $(".J_attackCode").click(function(e) {
                                e.preventDefault(),
                                    $("#J_attackCode").attr({
                                        src: BASE_PATH + "/transaction/captcha-image?goods_serial_num=" + t + "&t=" + (new Date).getTime()
                                    })
                            }),
                                $(".btn-normal").addClass("ui-btn-large")
                        }),
                        e.after("hide", function() {
                            this.element.remove()
                        }),
                        e.show(),
                        $("body").bind("loginBox", function() {
                            e.hide()
                        }),
                        !1
                })
        }
    )();
    var detailTimeDown = $(".less-than-day").length && Widget.extend({
        element: ".goods-info",
        setup: function() {
            this._timeDown()
        },
        attrs: {
            second: $(".less-than-day").data().second,
            type: $(".less-than-day").data().type,
            time: $(".less-than-day")
        },
        _zero: function(e) {
            var e = parseInt(e, 10);
            return e > 0 ? (9 >= e && (e = "0" + e),
            e + "") : "00"
        },
        _timeDown: function() {
            var e = this
                , t = e.get("type")
                , o = e.get("second");
            0 == o || 0 > o && t ? ("selling" === t && (e.get("time").html("\u5df2\u5230\u671f"),
                $(".btn-buy"),
                $(".btn-buy").removeClass("btn-buy").addClass("btn-out-time").html('<span class="span">\u5df2\u5230\u671f</span>'),
                $("#buySubmit,#loginToBuy").unbind("click").click(function() {
                    ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u6b64\u5546\u54c1\u5df2\u5230\u671f\uff0c\u65e0\u6cd5\u8d2d\u4e70\u3002")
                })),
            "public" === t && (e.get("time").html("\u516c\u793a\u5b8c\u6210"),
                $("#hideBtn").show())) : (e.set("second", --o),
                e._changeTime(e.get("time"), o),
                window.setTimeout(function() {
                    e._timeDown()
                }, 1e3))
        },
        _changeTime: function(e, t) {
            var o = this._zero(Math.floor(Number(t) / 3600))
                , n = this._zero(Math.floor(Number(t) / 60 % 60))
                , a = this._zero(Number(t) % 60);
            e.html(o + "\u5c0f\u65f6" + n + "\u5206\u949f" + a + "\u79d2")
        }
    });
    $(".less-than-day").length && (new detailTimeDown).render();
    var btnCollect = function(that, url, data, operate, className, title, favor) {
        $.post(url, {
            goods_id: data.goodsId,
            favor_id: data.favorId,
            flag: data.flag
        }, function(result) {
            if (data.submitFlag = "true",
                result = eval("(" + result + ")"),
                Number(result.code))
                that.className = className,
                    that.title = title,
                    data.isFavor = favor,
                    data.favorId = result.code;
            else if ("success" == result.code)
                that.className = className,
                    that.title = title,
                    data.isFavor = favor,
                    1 == favor ? ($(".m-follow-tip").show(400),
                        setTimeout(function() {
                            $(".m-follow-tip").hide()
                        }, 1e3)) : ($(".m-follow-plus-tip").show(400),
                        setTimeout(function() {
                            $(".m-follow-plus-tip").hide()
                        }, 1e3));
            else if ("fail" == result.code)
                ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c" + operate + "\u5931\u8d25\u3002");
            else if ("have_favored" == result.code)
                ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u60a8\u5df2\u5173\u6ce8\u8be5\u5546\u54c1\u3002"),
                    updateCollect($(that));
            else if (-1 !== (result.code + "").indexOf("too_much")) {
                var max = result.code.split("_")[2];
                max = Number(max) || 50,
                    ConfirmBox.confirm("\u60a8\u5173\u6ce8\u7684\u5546\u54c1\u603b\u6570\u5df2\u8fbe" + max + '\u6761\uff0c\u662f\u5426\u5220\u9664\u6700\u540e\u4e00\u6761\u8bb0\u5f55\u4ee5\u4fdd\u5b58\u6700\u65b0\u5173\u6ce8\u5546\u54c1\uff1f<br><br><input class="ui-checkbox14" name="noRemind" type="checkbox">\u4e0d\u518d\u63d0\u9192', "\u63d0\u793a", function() {
                        data.flag = !0,
                        $("[name='noRemind']").prop("checked") && Cookie.set("CygFavorRemind", "CygFavorRemind"),
                            $("[name='noRemind']").remove(),
                            btnCollect(that, url, data, operate, className, title, favor)
                    })
            } else
                ConfirmBox.warning("\u5bf9\u4e0d\u8d77\uff0c\u670d\u52a1\u9519\u8bef\u3002", "", function() {
                    window.location.reload()
                });
            that.innerHTML = "( " + result.count + " )"
        })
    };
    $(function() {
        var e = $(".btn-collect");
        e.length && (updateCollect(e),
        "loginToCollect" !== e.attr("id") && e.click(function() {
            var e, t, o, n, a, i = this, s = $(i).data();
            return s.flag = "CygFavorRemind" == Cookie.get("CygFavorRemind") ? !0 : !1,
                "false" === s.submitFlag ? !1 : (1 === Number(s.isFavor) ? (e = BASE_PATH + "/goods/removefavorgood",
                    t = "\u53d6\u6d88\u5173\u6ce8",
                    n = "\u70b9\u51fb\u5173\u6ce8\u5546\u54c1",
                    o = "btn-collect",
                    a = "0") : (e = BASE_PATH + "/goods/favorgoods",
                    t = "\u5173\u6ce8",
                    n = "\u70b9\u51fb\u53d6\u6d88\u5173\u6ce8",
                    o = "btn-collect collect-active",
                    a = "1"),
                    s.submitFlag = "false",
                    btnCollect(i, e, s, t, o, n, a),
                    void 0)
        }))
    })
});
